<template>
  <div class="">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="100px">
      <el-form-item label="抽检批次号" prop="batchNumber">
        <el-input v-model="queryParams.batchNumber" placeholder="请输入抽检批次号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="样品编号" prop="sampleNumber">
        <el-input v-model="queryParams.sampleNumber" placeholder="请输入样品编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="检验时间" prop="inspectTime">
        <el-date-picker clearable v-model="queryParams.inspectTime"
                        type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择检验时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-if="editFlag"
                   v-hasPermi="['develop:FactoryTestMachineryHammer:add']">新增机械试验(防振锤)
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品" align="center" prop="goodsName"/>
      <el-table-column label="抽检批次号" align="center" prop="batchNumber"/>
      <el-table-column label="样品编号" align="center" prop="sampleNumber"/>
      <el-table-column label="检验时间" align="center" prop="inspectTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="invoice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.testPass"/>
        </template>
      </el-table-column>
      <el-table-column label="导线标称外径" align="center" prop="outsideDiameter"/>
      <el-table-column label="锤头对钢绞线握力(要求值)" align="center" prop="headStrengthRequire"/>
      <el-table-column label="锤头对钢绞线握力(实测值)" align="center" prop="headStrengthTest"/>
      <el-table-column label="线夹对钢绞线握力(要求值)" align="center" prop="clampStrengthRequire"/>
      <el-table-column label="线夹对钢绞线握力(实测值)" align="center" prop="clampStrengthTest"/>
      <el-table-column prop="reportFile" label="质检报告" align="center">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.reportFile"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="editFlag"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:FactoryTestMachineryHammer:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" v-if="editFlag"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:FactoryTestMachineryHammer:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <factoryTestMachineryHammer-form ref="form" v-bind="$attrs" @ok="getList"></factoryTestMachineryHammer-form>
    <factory-test-machinery-hammer-detail ref="detail" v-bind="$attrs" @ok="getList"></factory-test-machinery-hammer-detail>

  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import FactoryTestMachineryHammerForm
  from '@/views/develop/factory/factoryTestMachineryHammer/module/FactoryTestMachineryHammerForm'
import FactoryTestMachineryStickDetail
  from "@/views/develop/factory/factoryTestMachineryStick/module/FactoryTestMachineryStickDetail.vue";
import FactoryTestMachineryHammerDetail
  from "@/views/develop/factory/factoryTestMachineryHammer/module/FactoryTestMachineryHammerDetail.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  name: "FactoryGoodsTestMachineryHammerList",
  dicts: ['yes_no'],
  components: {
    FileUploadShow,
    FactoryTestMachineryHammerDetail,
    FactoryTestMachineryStickDetail,
    FactoryTestMachineryHammerForm,
  },
  props: {
    workOrderId: {
      type: String,
      default: null
    },
    goodsId: {
      type: String,
      default: null
    },
    daohuoDetailId: {
      type: String,
      default: null
    },
    rukuId: {
      type: String,
      default: null
    },
    goodsSaleId: {
      type: String,
      default: null
    },
    produceRecordDetailId: {
      type: String,
      default: null
    },
    fahuoDetailId: {
      type: String,
      default: null
    },
    sampleId: {
      type: String,
      default: null
    },
    editFlag:{
      type:Boolean,
      default:false
    },
  },
  watch: {
    workOrderId: {
      handler(newVal, oldVal) {
        this.queryParams.workOrderId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    daohuoDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.daohuoDetailId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    rukuId: {
      handler(newVal, oldVal) {
        this.queryParams.rukuId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },

    goodsSaleId: {
      handler(newVal, oldVal) {
        this.queryParams.goodsSaleId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    produceRecordDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.produceRecordDetailId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

    fahuoDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.fahuoDetailId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

    sampleId: {
      handler(newVal, oldVal) {
        this.queryParams.sampleId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出厂试验-机械试验-防振锤表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        orderId: null,
        goodsId: null,
        goodsName: null,
        goodsModel: null,
        batchNumber: null,
        sampleNumber: null,
        inspectTime: null,
        outsideDiameter: null,
        headStrengthRequire: null,
        headStrengthTest: null,
        clampStrengthRequire: null,
        clampStrengthTest: null,
      },
      url: {
        list: '/factoryTestMachineryHammer/factoryTestMachineryHammer/list',
        remove: '/factoryTestMachineryHammer/factoryTestMachineryHammer/',
        export: '/factoryTestMachineryHammer/factoryTestMachineryHammer/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询出厂试验-机械试验-防振锤列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询生产过程数据列表 */
    getListByFahuoDetailId() {
      this.loading = false;
      getAction(this.url.listByFahuoDetail, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      let workOrderId = this.workOrderId;
      let goodsId = this.goodsId;
      let daohuoDetailId = this.daohuoDetailId;
      let rukuId = this.rukuId;
      let goodsSaleId = this.goodsSaleId;
      let produceRecordDetailId = this.produceRecordDetailId;
      let fahuoDetailId = this.fahuoDetailId;
      let sampleId = this.sampleId;
      if (workOrderId) {
        this.$refs.form.handleAddByWorkOrderId(workOrderId,goodsId);
        return
      }
      if (daohuoDetailId) {
        this.$refs.form.handleAddByDaohuoDetailId(daohuoDetailId,goodsId);
        return
      }
      if (produceRecordDetailId) {
        this.$refs.form.handleAddByProduceDetailId(produceRecordDetailId,goodsId,goodsSaleId);
        return
      }
      if (fahuoDetailId) {
        this.$refs.form.handleAddByFahuoDetailId(fahuoDetailId);
        return
      }
      if (rukuId) {
        this.$refs.form.handleAddByRukuId(rukuId,goodsId);
        return
      }
      if (goodsSaleId) {
        this.$refs.form.handleAddByGoodsSaleId(goodsSaleId,goodsId);
        return
      }
      if (sampleId) {
        this.$refs.form.handleAddBySampleId(sampleId,goodsId,goodsSaleId);
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.$refs.detail.showDetail(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `factoryTestMachineryHammer_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
